//
//  UIViewController+YWCategoryPicker.m
//  YWBiubiu
//
//  Created by NeiQuan on 16/8/15.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "UIViewController+YWCategoryPicker.h"
#import <UIKit/UIKit.h>
#include <objc/runtime.h>
 static  UIDatePicker     *_ywdatePickerView;
 static  UIButton         *_ywcoverPickerButton;
 static  UIPickerView     *_ywsexPickerView;
 static  UIView           *_ywPickerbackView;
 static  UILabel          *_ywtitleLable;
 static  NSString         *_ywsexString;
 YWCategoryPickerType    _ywinnerType;
 static  const  void *YWCategorykey= &YWCategorykey;
@implementation UIViewController (YWCategoryPicker)
#pragma mark --创建对象
-(void)viewControllerPOPPickerType:(YWCategoryPickerType)type withsuccessBlock:(YWCategoryPickerBlock)block
{
    _ywinnerType=type;
    _ywsexString=@"男";
    [self createSubViews];
    [self makePickerView:type];
    objc_setAssociatedObject(self, YWCategorykey, block, OBJC_ASSOCIATION_COPY);
}
#pragma mark --子对象
-(void)createSubViews
{
    __weak typeof(self)weakself=self;
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIButton *cover=[ UIButton buttonWithType:UIButtonTypeCustom];
    cover.frame=window.bounds;
    [cover setBackgroundColor:[ UIColor colorWithWhite:0.5 alpha:0.7]];
    [cover addTarget:weakself action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
    _ywcoverPickerButton=cover;
 

    UIView *backView=[[ UIView alloc] initWithFrame:CGRectMake(0, kMainScreenHeight-200, kMainScreenWidth, 200)];
    [backView setBackgroundColor:[ UIColor whiteColor]];
    
    
    UIView  *toolView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 30)];
    UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake((kMainScreenWidth-70)*0.5, 5, 70, 21)];
    lable.text=@"标题";
    lable.font=[ UIFont systemFontOfSize:13];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.textColor=[ UIColor blackColor];
    _ywtitleLable=lable;
    
    UIButton *cancleButton=[ UIButton buttonWithType:UIButtonTypeCustom];
    cancleButton.frame=CGRectMake(5, 3, 50, 25);
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    cancleButton.titleLabel.font=[ UIFont systemFontOfSize:13];
    [cancleButton setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    [cancleButton addTarget:weakself action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sureButton=[ UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.frame=CGRectMake(kMainScreenWidth-60, 3, 50, 25);
    sureButton.titleLabel.font=[ UIFont systemFontOfSize:13];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    [sureButton addTarget:weakself action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    
    [toolView addSubview:lable];
    [toolView addSubview:cancleButton];
    [toolView addSubview:sureButton];
    [backView addSubview:toolView];
    
    [cover addSubview:backView];
    _ywPickerbackView=backView;
    [window addSubview:cover];
    
}
-(void)makePickerView:(YWCategoryPickerType)type
{
  
    UIDatePicker *datePicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 32, kMainScreenWidth, 200-35)];
    datePicker.backgroundColor = [UIColor whiteColor];
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    datePicker.backgroundColor = [UIColor colorWithString:@"f6f6f6"];
    datePicker.datePickerMode = UIDatePickerModeDate;
    _ywdatePickerView=datePicker;
    
    UIPickerView *picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 32, kMainScreenWidth,  200-35)];
    picker.delegate=self;
    picker.dataSource=self;
    
    switch (type)
    {
        case YWCategoryPickerAgeType:
        {
            [_ywPickerbackView addSubview:datePicker];
             _ywtitleLable.text=@"日期";
        }
            break;
        case YWCategoryPickerSexType:
        {
          [_ywPickerbackView addSubview:picker];
          _ywtitleLable.text=@"性别";
        }
            break;
            
        default:
            break;
    }
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
  
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 2;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return row==0? @"男":@"女";

}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _ywsexString=row==0?@"男":@"女";
}
#pragma mark --点击确定按钮
-(void)sureClick
{
   [_ywcoverPickerButton removeFromSuperview];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-MM-dd";
    NSString *dateStr =[formatter stringFromDate:_ywdatePickerView.date];
    YWCategoryPickerBlock Block=objc_getAssociatedObject(self, YWCategorykey);
    
    switch (_ywinnerType) {
        case YWCategoryPickerAgeType:
         Block(dateStr);
            break;
        case YWCategoryPickerSexType:
          Block(_ywsexString);
          break;
        default:
            break;
    }
}
-(void)coverClick
{
    [_ywcoverPickerButton removeFromSuperview];
}
@end
